// pages/home/home.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    type: 3,//专区 1 精选专区 2  配件专区 3 饮料专区 4 频谱专区
    currentTab: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //判断是否为iPhone X
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          _this.setData({
            isIphoneX: true
          })
        }
      }
    })
    let _this = this
    $api.getPosts({
    }).then((res) => {
      let Posts = res.data
      _this.setData({
        Posts,
        modalName: Posts.data_type == 2 ? 'liveimg' : ''
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
    $api.getBanners({
    }).then((res) => {
      _this.setData({
        banner_info: res.data.data
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
    $api.getindexzq({
    }).then((res) => {
      let zq_id = res.data.data.zq2_info[0].id
      _this.setData({
        zq1_info: res.data.data.zq1_info,
        zq2_info: res.data.data.zq2_info,
        zq_id,
      })
      
      this.getptgoodszq()
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  getptgoodszq() {
    let _this = this
    $api.getindexzqdata({
      zq_id: _this.data.zq_id
    }).then((res) => {

      _this.setData({
        list: res.data.data
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },
  GoodType(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      currentTab: e.currentTarget.dataset.index,
      zq_id: e.currentTarget.dataset.id,
    })
    this.getptgoodszq()
  },
  Readingdetails(e) {
    var _this = this;
    let is_register = e.currentTarget.dataset.is_register
    if(is_register == 2){
      wx.navigateTo({
        url: '/pages/Readingdetails/Readingdetails?pt=1&id=' + e.currentTarget.dataset.id,
      })
    }else if(is_register == 0){
      wx.navigateTo({
        url: '/pages/Readingdetails/Readingdetails?ep=1&id=' + e.currentTarget.dataset.id,
      })
    }
    
  },
  gotypegoods(e) {
    console.log(e)
    var _this = this;
    wx.navigateTo({
      url: '/pages/typegoods/typegoods?id=' + e.currentTarget.dataset.id + '&btname=' + e.currentTarget.dataset.zq_name,
    })
  },

  gosearchgoods() {
    let _this = this
    wx.navigateTo({
      url: '/pages/searchgoods/searchgoods?sousuoType=home',
    })
  },
  //进入首页
  onIndex() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  //进入我的
  onMine() {
    wx.reLaunch({
      url: '/pages/mine/mine',
    })
  },
  // 进入直播
  onlive() {
    wx.reLaunch({
      url: '/pages/live/live',
    })
  },
  //关闭活动弹出图
  hideModal(e) {
    this.setData({
      modalName: null,
      image_info: [],
      bannerOpen: false
    })
  },
  onSearch(e) {
    let data = e.currentTarget.dataset
    let _this = this
    if (data.banner_type == 1) {
      wx.navigateTo({
        url: '/pages/typegoods/typegoods?banner=1&&id=' + data.id + '&btname=' + data.title,
      })
    } else if (data.banner_type == 2) {
      $api.getBannersdata({
        banner_id: data.id
      }).then((res) => {
        _this.setData({
          image_info: res.data.data.image_info,
          bannerOpen: true
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }

  },
  // 加入购物车
  oncart(e){
    let data = e.currentTarget.dataset.item
    console.log(data)
    let buy_goods = [{
      good_id:data.good_id,
      num:1
    }]
    $api.addshoppingcart({
      buy_goods: JSON.stringify(buy_goods),
      mode:data.is_register,
      type:1
    }).then((res) => {
      if(res.data.code == 0){
        wx.showToast({
          title: '已加入购物车',
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  // 跳转购物车
  gocart(){
    wx.navigateTo({
      url: '/pages/cart/cart'
    })
  }
})
